/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package milkyway.hightech.entry;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormat;
import java.util.Date;
import milkyway.hightech.money.Money;
import milkyway.hightech.timer.TimeEmitter;

/**
 *
 * @author michael
 */
public final class Entry
{
    /**
     * the description of this transfer
     */
    private final String reasonOfTransfer;

    /**
     * the amount of money
     */
    private final Money money;

    private char sign;

    private final Date date;
    private final String dateAsString;

    /**
     * creates a new entry
     * @param reasonOfTransfer the dectription of this transfer
     * @param amount the amount of money
     */
    public Entry(String reasonOfTransfer, char sign, Money money)
    {
	this.money = money;

	this.reasonOfTransfer = toFixedString(reasonOfTransfer, 30);
	this.sign = sign;

	DateFormat dateFormatter = DateFormat.getDateInstance();
	DateFormat timeFormatter = DateFormat.getTimeInstance();

	date = TimeEmitter.getInstance().getCalendar().getTime();
	
	dateAsString = dateFormatter.format(date) + ", "
		+ timeFormatter.format(date) + " Uhr";
    }

    /**
     * trims the given string into the given size
     * @param string
     * @param size
     * @return 
     */
    private String toFixedString(String string, int size)
    {
	char[] reason = new char[size];

	for (int i = 0; i < reason.length; i++)
	    reason[i] = ' ';

	if(string.toCharArray().length > reason.length)
	    System.arraycopy(string.toCharArray(), 0,
		    reason, 0, reason.length);
	else
	    System.arraycopy(string.toCharArray(), 0,
		    reason, 0, string.toCharArray().length);

	return String.valueOf(reason);
    }

    /**
     * trims the given BigInteger into the given size
     * @param amount
     * @param size
     * @return 
     */
    private String toFixedString(BigInteger amount, int size)
    {
	char[] reason = new char[size];

	for (int i = 0; i < reason.length; i++)
	    reason[i] = '_';

	if(amount.toString().toCharArray().length > reason.length)
	    System.arraycopy(amount.toString().toCharArray(), 0,
		    reason, 0, reason.length);
	else
	    System.arraycopy(amount.toString().toCharArray(), 0,
		    reason, 0, amount.toString().toCharArray().length);

	return String.valueOf(reason);
    }

    /**
     * gets a string-object of this transfer
     * @return
     */
    @Override
    public String toString()
    {
	BigDecimal d = new BigDecimal(money.getValue()).divide(BigDecimal.valueOf(100));

	String data = "Reason of transfer: " + reasonOfTransfer
		+ "\tamount: " + sign + d + " " + money.getCurrency()
		+ "\tdate of transfer: " + dateAsString + "\n";

	return data;
    }

    public Date getDate()
    {
	return date;
    }

    public Money getMoney()
    {
	return money;
    }

    public String getReasonOfTransfer()
    {
	return reasonOfTransfer;
    }
}
